#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <hiredis/hiredis.h>

#include "m_mssql.h"

void redis_check(char *redis_addr, int redis_port, char *redis_auth)
{
  redisContext *context = NULL;
  redisReply *reply = NULL;
  char buff[0xFF];
  struct timeval timeout = { 1, 500000 };

  if ( NULL == redis_addr)
    return ;

  context = redisConnectWithTimeout((char *) redis_addr, redis_port, timeout);
  if (context->err)
  {
    sprintf(buff, "redis %s", context->errstr);
    sql_server(redis_addr, buff, 0);
    goto end;
  }

  if (redis_auth == NULL)
    goto end;

  reply = (redisReply *) redisCommand(context, "AUTH %s", redis_auth);
  if (reply == NULL)
  {
    sprintf(buff, "redis auth failed.");
    sql_server(redis_addr, buff, 0);
    goto end;
  }

end:
  if (reply)
  {
    freeReplyObject(reply);
    reply = NULL;
  }

  if (context)
  {
    redisFree(context);
    context = NULL;
  }
  return ;
}
